package com.example.backend.api.controller;


import com.example.backend.api.common.Result;
import com.example.backend.api.service.impl.AttendanceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.ParseException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/api/attendance")
@CrossOrigin(origins = "*", maxAge = 3600)
public class AttendanceController {

    @Autowired
    private AttendanceServiceImpl attendanceService;

    @GetMapping("getall")
    public Result getAll(@RequestParam(value = "status", required = false) String status,
                         @RequestParam(value = "dept_id", required = false) Integer deptId
    ) {
        return attendanceService.getAll(status, deptId);
    }

    @GetMapping("delete")
    public Result delete(@RequestParam("id") String id) {
        return attendanceService.delete(Integer.parseInt(id));
    }

    @PostMapping("update")
    public Result update(
            @RequestParam("user_id") Integer id,
            @RequestParam(value = "dept_id",required = false) String dept_id,
            @RequestParam(value = "attendant_status",required = false) String status,
            @RequestParam(value = "start_time",required = false) String startTime,
            @RequestParam(value = "end_time",required = false) String endTime
            ) throws ParseException {
        return attendanceService.update(id,dept_id,status,startTime,endTime);
    }

    @PostMapping("check")
    public Result check(
            @RequestParam("user_id") Integer id,
            @RequestParam(value = "start_time" ) String startTime,
            @RequestParam(value = "end_time",required = false) String endTime,
            @RequestParam(value = "dept_id") Integer dept_id) throws ParseException{
        return attendanceService.check(id,startTime,endTime,dept_id);
    }


    @GetMapping("info")
    public Result getAttendanceInfo(@RequestParam(value = "start") String start,
                                    @RequestParam(value = "end") String end) throws ParseException {

        return attendanceService.getAttendanceInfo(start,end);
    }

}
